package com.gordon.wiki.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gordon.wiki.pojo.Ebook;
import com.gordon.wiki.req.EbookQueryReq;
import com.gordon.wiki.req.EbookSaveReq;
import com.gordon.wiki.resp.EbookResp;
import com.gordon.wiki.resp.PageResp;
import com.gordon.wiki.result.Result;
import com.gordon.wiki.result.ResultCode;
import com.gordon.wiki.result.ResultUtil;
import com.gordon.wiki.service.EbookService;
import com.gordon.wiki.utils.CopyUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Projectname: wiki
 * @Filename: EbookController
 * @Author: GordonLo
 * @Data:2022/11/8 14:45
 * @Description: TODO
 */
@RestController
@RequestMapping("/ebook")
public class EbookController {

    //打印日志
    private static final Logger LOG = LoggerFactory.getLogger(EbookService.class);

    @Autowired
    private EbookService ebookService;

//    @GetMapping("/getEbooks")
//    public List<Ebook> getEbooks() {
//        List<Ebook> list = ebookService.list();
//        for (Ebook ebook : list) {
//            System.out.println(ebook.toString());
//        }
//        return list;
//    }

    @GetMapping("/getEbooks")
    public Result getEbooks() {
        List<Ebook> list = ebookService.list();
        for (Ebook ebook : list) {
            System.out.println(ebook.toString());
        }
        return ResultUtil.success(ResultCode.SUCCESS, list);
    }

    @GetMapping("/getEbookByName")
    public Result getEbookByName(String name) {
        QueryWrapper<Ebook> wrapper = new QueryWrapper<>();
        wrapper.like("name", name);
        List<Ebook> list = ebookService.list(wrapper);
        return ResultUtil.success(ResultCode.SUCCESS, list);
    }


//    @GetMapping("/getEbookByEbookReq")
//    public Result getEbookByEbookReq(EbookReq req) {
//        QueryWrapper<Ebook> wrapper = new QueryWrapper<>();
//        wrapper.like("name", req.getName());
//        List<Ebook> list = ebookService.list(wrapper);
//
////        ArrayList<EbookResp> ebookResps = new ArrayList<>();
////        for (Ebook ebook : list) {
////            EbookResp copy = CopyUtil.copy(ebook, EbookResp.class);
////            ebookResps.add(copy);
////        }
//        List<EbookResp> ebookResps = CopyUtil.copyList(list, EbookResp.class);
////        CommonResp<List<EbookResp>> resp = new CommonResp<>();
////        resp.setContent(ebookResps);
//        return ResultUtil.success(ResultCode.SUCCESS, ebookResps);
//    }

    @GetMapping("/getEbookByEbookReq")
    public Result getEbookByEbookReq(EbookQueryReq req) {
        List<Ebook> list;
        System.out.println("req = " + req);
        if (!ObjectUtils.isEmpty(req.getName())) {
            QueryWrapper<Ebook> wrapper = new QueryWrapper<>();
            wrapper.like("name", req.getName());
            list = ebookService.list(wrapper);
        } else {
            list = ebookService.list();
        }
        List<EbookResp> ebookResps = CopyUtil.copyList(list, EbookResp.class);
//        CommonResp<List<EbookResp>> resp = new CommonResp<>();
//        resp.setContent(ebookResps);
        return ResultUtil.success(ResultCode.SUCCESS, ebookResps);
    }

    @GetMapping("/getEbookListByPage")
    //public Result getEbookListByPage(@RequestParam("current") int current, @RequestParam("size") int size) {
    public Result getEbookListByPage(@Valid EbookQueryReq req) {

        Page<Ebook> page = new Page<>(req.getPage(), req.getSize());

        QueryWrapper<Ebook> wrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(req.getCategory2_id())) {
            wrapper.like("category2_id", req.getCategory2_id());
        }
        if (!ObjectUtils.isEmpty(req.getName())) {
            wrapper.like("name", req.getName());
        }

        page = ebookService.page(page, wrapper);

        List<Ebook> list = page.getRecords();

        LOG.info("总行数：{}", page.getTotal() + "");
        LOG.info("总页数：{}", page.getPages() + "");

        List<EbookResp> ebookResps = CopyUtil.copyList(list, EbookResp.class);

        PageResp<EbookResp> respPageResp = new PageResp<>();
        respPageResp.setTotal(page.getTotal());
        respPageResp.setList(ebookResps);

//        CommonResp<Page<Ebook>> resp = new CommonResp<>();
//        resp.setContent(page);
        return ResultUtil.success(respPageResp);
    }


    @PostMapping("/save")
    public Result save(@Valid @RequestBody EbookSaveReq req) {

//        LOG.info("请求参数：{}", req.toString() + "");
//
//
//        System.out.println("请求参数：{}" + req.toString());

        Ebook ebook = CopyUtil.copy(req, Ebook.class);
        //会根据是否有id判断   修改（有id）  新增（无id）
        ebookService.saveOrUpdate(ebook);

        return ResultUtil.success();
    }


    @DeleteMapping("/remove")
    public Result remove(int id) {

        boolean b = ebookService.removeById(id);
        if (b) {
            return ResultUtil.success();
        } else {
            return ResultUtil.failure();
        }
    }

}